package com.sigmatrix.h5.service.common;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.baomidou.mybatisplus.toolkit.StringUtils;
import com.sigmatrix.h5.config.SpringContextHelper;
import com.sigmatrix.h5.properties.ServiceProperties;
import com.sigmatrix.h5.service.ShortMessageService;

/**
 * 
 * 
 * @author wei.wang
 *
 */
@Component
public class ServiceHelper {
    
	@Autowired
	private ServiceProperties serviceConfig;
	
	/**
	 * 获取短信发送service
	 * 
	 * @param eseCode
	 * @return
	 */
	//TODO 临时做法。需要配置化企业code-service关系
	public static ShortMessageService selectSmsService(String eseCode) {
		if("JGJ".equalsIgnoreCase(eseCode)){
			return SpringContextHelper.context.getBean("shortMessageServiceV2", ShortMessageService.class);
		}
		if("JLJ001".equalsIgnoreCase(eseCode)){
			return SpringContextHelper.context.getBean("shortMessageServiceV2", ShortMessageService.class);
		}
		if("jiuguijiu".equalsIgnoreCase(eseCode)){
			return SpringContextHelper.context.getBean("shortMessageServiceV2", ShortMessageService.class);
		}
		return SpringContextHelper.context.getBean("shortMessageServiceV1", ShortMessageService.class);
		
	}
	
	/**
	 * 根据企业code和service类型获取bean。<br>
	 * 如果 defaultSwitch == true时，当不存在指定的bean时，返回默认的值。
	 * @param eseCode
	 * @param requiredType
	 * @param defaultSwitch 如果为true时，当不存在指定的bean时，返回默认的值。
	 * @return
	 */
	public <T> T selectService(String eseCode,Class<T> requiredType,boolean defaultSwitch) {
		String specifiedBeanName = serviceConfig.selectServiceName(eseCode, requiredType);
		if(StringUtils.isEmpty(specifiedBeanName)){
			specifiedBeanName = serviceConfig.selectDefaultServiceName(requiredType);
		}
		if(StringUtils.isEmpty(specifiedBeanName)){
			return SpringContextHelper.context.getBean(requiredType);			
		}
		return SpringContextHelper.context.getBean(specifiedBeanName, requiredType);
	}
	
	/**
	 * 根据企业code和service类型获取bean
	 * @param eseCode
	 * @param requiredType
	 * @return
	 */
	public <T> T selectService(String eseCode,Class<T> requiredType) {
		String specifiedBeanName = serviceConfig.selectServiceName(eseCode, requiredType);
		return SpringContextHelper.context.getBean(specifiedBeanName, requiredType);
	}
	
	
	/**
	 * 根据service类型获取默认bean
	 * @param requiredType
	 * @return
	 */
	public <T> T selectDefaultService(Class<T> requiredType) {
		String specifiedBeanName = serviceConfig.selectDefaultServiceName(requiredType);
		return SpringContextHelper.context.getBean(specifiedBeanName, requiredType);
	}
	
	/**
	 * 判断当前是不是酒鬼酒企业
	 * 
	 * @param eseCode
	 * @return
	 */
	public static boolean isJiuGuiJiu(String eseCode) {
		if("JGJ".equalsIgnoreCase(eseCode)){
			return true;
		}
		if("JLJ001".equalsIgnoreCase(eseCode)){
			return true;
		}
		if("jiuguijiu".equalsIgnoreCase(eseCode)){
			return true;
		}
		return false;
	}
	
}
